package com.gp.pj.controller;


import com.alibaba.fastjson.JSONObject;
import com.gp.pj.entity.DictItem;
import com.gp.pj.service.DictItemService;
import com.gp.pj.utils.ResultUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Chloe
 * @since 2020-12-14
 */
@RestController
@RequestMapping("/")
public class DictItemController {

    @Resource
    DictItemService dictItemService;

    @ResponseBody
    @GetMapping( value = "/getDictItemList")
    public ResultUtils dictItemList(String dictId) {
        return dictItemService.getDictItemList(dictId);
    }

    @ResponseBody
    @PostMapping( value = "saveDictItem")
    public ResultUtils saveDictItem(@RequestBody JSONObject jsonRe){
        DictItem dictItem = jsonRe.toJavaObject(DictItem.class);
        boolean save = dictItemService.save(dictItem);
        if (save) {
            return ResultUtils.success();
        } else {
            return ResultUtils.failure("新增字典信息失败");
        }
    }

    @ResponseBody
    @PostMapping( value = "updateDictItem")
    public ResultUtils updateDictItem(@RequestBody JSONObject jsonRe){
        DictItem dictItem = jsonRe.toJavaObject(DictItem.class);
        boolean update = dictItemService.updateById(dictItem);
        if (update) {
            return ResultUtils.success();
        } else {
            return ResultUtils.failure("更新字典信息失败");
        }
    }

    @ResponseBody
    @PostMapping( value = "deleteDictItem")
    public ResultUtils deleteDictItem(@RequestBody JSONObject jsonRe){
        DictItem dictItem = jsonRe.toJavaObject(DictItem.class);
        boolean remove = dictItemService.removeById(dictItem);
        if (remove) {
            return ResultUtils.success();
        } else {
            return ResultUtils.failure("更新字典信息失败");
        }
    }

}
